package com.zat.mes.andrmes.dofun.system

import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.google.gson.Gson
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import com.zat.mes.andrmes.utils.Md5Util
import com.zat.mes.andrmes.utils.PreferencesUtil
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_update_pwd.*
import kotlinx.android.synthetic.main.layout_title.*
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * 修改密码activity
 */
class UpdatePwdActivity : BaseActivity() {
    override fun init() {
        initView()
        addListener()
    }

    /**
     * 添加监听
     */
    private fun addListener() {
        cbYUpdatePwd.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                etYboxUpdatePwd.setTransformationMethod(
                    HideReturnsTransformationMethod
                        .getInstance()
                )

            } else {
                etYboxUpdatePwd.setTransformationMethod(
                    PasswordTransformationMethod
                        .getInstance()
                );
            }
            etYboxUpdatePwd.setSelection(etYboxUpdatePwd.text.length)


        }
        cbNUpdatePwd.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                etNboxUpdatePwd.setTransformationMethod(
                    HideReturnsTransformationMethod
                        .getInstance()
                );
            } else {
                etNboxUpdatePwd.setTransformationMethod(
                    PasswordTransformationMethod
                        .getInstance()
                );
            }
            etNboxUpdatePwd.setSelection(etNboxUpdatePwd.text.length)
        }
        cbQUpdatePwd.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                etQboxUpdatePwd.setTransformationMethod(
                    HideReturnsTransformationMethod
                        .getInstance()
                );
            } else {
                etQboxUpdatePwd.setTransformationMethod(
                    PasswordTransformationMethod
                        .getInstance()
                );
            }
            etQboxUpdatePwd.setSelection(etQboxUpdatePwd.text.length)
        }
        btnUpdatePwd.setOnClickListener {
            if (TextUtils.isEmpty(etYboxUpdatePwd.text.toString())) {
                showToast(getString(R.string.text_input_old_pwd))
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etNboxUpdatePwd.text.toString())) {
                showToast(getString(R.string.text_input_new_pwd))
                return@setOnClickListener
            }
            if (etNboxUpdatePwd.text.toString().length < 8) {
                showToast(getString(R.string.text_input_new_pwd_check))
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etQboxUpdatePwd.text.toString())) {
                showToast(getString(R.string.text_input_check_pwd))
                return@setOnClickListener
            }
            if (etQboxUpdatePwd.text.toString().length < 8) {
                showToast(getString(R.string.text_input_check_pwd_check))
                return@setOnClickListener
            }
            if (!etNboxUpdatePwd.text.toString().equals(etQboxUpdatePwd.text.toString())) {
                showToast(getString(R.string.text_input_two_pwd_same))
                return@setOnClickListener
            }

            //请求接口
            loadUpate()
        }

    }

    private fun initView() {
        setContentView(R.layout.activity_update_pwd)
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }

    }

    /**
     * 修改密码
     */
    private fun loadUpate() {
        showLoading()
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        val empId = loginResultBean.empid
        val jsonObject = JSONObject()
        jsonObject.put("id", empId)
        jsonObject.put("oldPwd", Md5Util.MD5(etYboxUpdatePwd.text.toString()))
        jsonObject.put("newPwd", Md5Util.MD5(etNboxUpdatePwd.text.toString()))
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .updatePwd(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    closeLoading()
                    if (response?.body()?.success != null) {
                        showToast(response?.body()?.message)
                        if (response.body()?.success!!) {
                            val loginParam = PreferencesUtil.getString(CompanionType.LOGIN_PARAM)
                            if (!TextUtils.isEmpty(loginParam)) {
                                try {
                                    val jsonObject = JSONObject(loginParam)
                                    jsonObject.put("password", etNboxUpdatePwd.text.toString())
                                    PreferencesUtil.saveValue(
                                        CompanionType.LOGIN_PARAM,
                                        jsonObject.toString()
                                    )
                                } catch (e: Exception) {
                                }

                            }
                            finish()
                        }
                    }
                }

            })
    }

}
